package com.gitee.mrj.aiboot.core.sf;


import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;

public final class FormDefinitionGeneratorFactory {

    public Optional<List<FormDefinitionGenerator>> getGenerator(Class clazz) {
        return Optional.ofNullable(GENERATORS.get(clazz));
    }

    void register(FormDefinitionGenerator generator) {
        Class[] supports = generator.supports();
        for (Class support : supports) {
            GENERATORS.add(support, generator);
        }
    }

    public static FormDefinitionGeneratorFactory getInstance() {
        if (instance == null) {
            instance = new FormDefinitionGeneratorFactory();
        }
        return instance;
    }

    private static final MultiValueMap<Class, FormDefinitionGenerator> GENERATORS = new LinkedMultiValueMap<>();

    private static FormDefinitionGeneratorFactory instance;

    private FormDefinitionGeneratorFactory() {
    }

}
